package cn.auction.web;

import cn.auction.biz.AuctionUserService;
import cn.auction.biz.impl.AuctionUserServiceImpl;
import cn.auction.entity.AuctionUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserAction {
    public UserAction() {
    }

    public String doRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            AuctionUser auctionuser = new AuctionUser();
            auctionuser.setUserName(request.getParameter("username"));
            auctionuser.setUserPassword(request.getParameter("userpassword"));
            auctionuser.setUserCardNo(request.getParameter("usercardno"));
            auctionuser.setUserTel(request.getParameter("usertel"));
            auctionuser.setUserAddress(request.getParameter("useraddress"));
            auctionuser.setUserPostNumber(request.getParameter("userpostnumber"));
            auctionuser.setUserIsAdmin(false);
            request.setAttribute("registerUser", auctionuser);
            Object numrand = request.getSession().getAttribute("numrand");//验证码
            if (numrand != null && numrand.equals(request.getParameter("inputCode"))) {
                AuctionUserService biz = new AuctionUserServiceImpl();
                biz.register(auctionuser);
                return "redirect:/login.jsp";
            } else {
                return "/register.jsp?msg=fail";
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            return "forward:/error.jsp";
        }
    }
    public String doAdminRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            AuctionUser auctionuser = new AuctionUser();
            auctionuser.setUserName(request.getParameter("username"));
            auctionuser.setUserPassword(request.getParameter("userpassword"));
            auctionuser.setUserCardNo(request.getParameter("usercardno"));
            auctionuser.setUserTel(request.getParameter("usertel"));
            auctionuser.setUserAddress(request.getParameter("useraddress"));
            auctionuser.setUserPostNumber(request.getParameter("userpostnumber"));
            auctionuser.setUserIsAdmin(true);
            request.setAttribute("registerUser", auctionuser);
            Object numrand = request.getSession().getAttribute("numrand");//验证码
            if (numrand != null && numrand.equals(request.getParameter("inputCode"))) {
                AuctionUserService biz = new AuctionUserServiceImpl();
                biz.register(auctionuser);
                return "redirect:/login-admin.jsp";
            } else {
                return "/admin-register.jsp?msg=fail";
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            return "forward:/error.jsp";
        }
    }


    public String doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String username = request.getParameter("username");
            String userpassword = request.getParameter("userpassword");
            request.setAttribute("username", username);
            request.setAttribute("userpassword", userpassword);
            if (!request.getSession().getAttribute("numrand").equals(request.getParameter("inputCode"))) {//如果验证码错误
                return "forward:/login.jsp?msg=NumError";
            } else {
                AuctionUserService biz = new AuctionUserServiceImpl();
                AuctionUser user = biz.login(username, userpassword);
                if (user == null) {
                    return "/login.jsp?msg=loginError";
                } else {
                    request.getSession().setAttribute("user", user);
                    return "redirect:/do/auction/auctionList";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            return "forward:/error.jsp";
        }
    }
    //管理员登录
    public String doAdminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String username = request.getParameter("username");
            String userpassword = request.getParameter("userpassword");
            request.setAttribute("username", username);
            request.setAttribute("userpassword", userpassword);
            if (!request.getSession().getAttribute("numrand").equals(request.getParameter("inputCode"))) {
                return "/login-admin.jsp?msg=NumError";
            } else {
                AuctionUserService biz = new AuctionUserServiceImpl();
                AuctionUser user = biz.adminlogin(username, userpassword);
                if (user == null) {
                    return "forward:/login-admin.jsp?msg=loginError";
                } else {
                    request.getSession().setAttribute("user", user);
                    return "forward:/do/auction/auctionList";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            return "/error.jsp";
        }
    }

    public String doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Boolean isAdmin = null;
            if (request.getSession().getAttribute("user") == null) {
                isAdmin = false;
            } else {
                isAdmin = ((AuctionUser)request.getSession().getAttribute("user")).getUserIsAdmin();
            }
            request.getSession().invalidate();
            return isAdmin ? "redirect:/login-admin.jsp" : "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            return "/error.jsp";
        }
    }
}
